package com.yyfax.ph.base;

import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import com.yyfax.common.adapter.BaseRecycleAdapter;
import com.yyfax.common.adapter.StatesAdapter;
import com.yyfax.common.recyclerview.*;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.ph.R;

/**
 * Created by HenryZhang on 2017/4/11.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public abstract class RecyclerListActivity extends BaseYYFaxActivity implements
		StatesAdapter.RetryClickListener, OnRefreshListener, OnLoadMoreListener, OnItemClickListener {

	public IRecyclerView mRecyclerView;
	public StatesAdapter mStatesAdapter;
	protected View mNoNetworkLayout;
	protected View mEmptyLayout;

	public int mPageNum = 1;
	public int mPageSize = 20;

	protected Divider mDivider;

	public void setup(RecyclerListActivity aty, int recyclerViewId, int spanCount, BaseRecycleAdapter adapter) {
		mRecyclerView = (IRecyclerView) findViewById(recyclerViewId);
		mRecyclerView.setLayoutManager(new GridLayoutManager(aty, spanCount));
		//mRecyclerView.addItemDecoration(new Divider(DensityUtil.dip2px(this,0.5f), getResources().getColor(R.color.color_e6e6e6), false, 0, 0, 0, 0));
		adapter.setOnItemClickListener(aty);
		setDefaultDataErrorLayout();
		mStatesAdapter = new StatesAdapter(adapter, null, mEmptyLayout, mNoNetworkLayout, mRecyclerView, aty);
//		mStatesAdapter = new StatesAdapter(mAdapter, mRecyclerView, this);
		mStatesAdapter.setRetryClickListener(aty);
		mRecyclerView.setIAdapter(mStatesAdapter);
		mRecyclerView.setOnRefreshListener(aty);
		mRecyclerView.setOnLoadMoreListener(aty);
		//mRecyclerView.setRefreshing(true);	子类去手动调用刷新操作
		mDivider = new Divider(DensityUtil.dip2px(this, 10f), getResources().getColor(R.color.color_f5f8ff),
				false, 0, 0, 0, DensityUtil.dip2px(this, 10f), 0);
		mRecyclerView.addItemDecoration(mDivider);
	}

	public void setDefaultDataErrorLayout(){
		mNoNetworkLayout = getLayoutInflater().inflate(R.layout.state_view_no_network, mRecyclerView, false);
		mEmptyLayout = getLayoutInflater().inflate(R.layout.state_view_no_data, mRecyclerView, false);
	}

//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.research_activity);
//		ButterKnife.bind(this);
//		mAdapter = new ResearchListAdapter();
//		Builder builder = new Builder();
//		builder.build(this, R.id.research_recyclerview, 1, mAdapter);
//		setTitle(R.string.research);
//	}

	@Override
	protected void onDestroy() {
		mStatesAdapter.dettach();
		super.onDestroy();
	}

	@Override
	public void onFunctionalError(Throwable e, boolean autoTips) {
		super.onFunctionalError(e, autoTips);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mRecyclerView.stopLoading();
				mRecyclerView.stopRefresh();
				mStatesAdapter.setState(StatesAdapter.STATE_ERROR);
			}
		});
	}
}
